<?php
	include("../csdl.php");
	$knoi=new csdl();
	if(isset($_GET['loai']))//div=id_loaitin
	{
		$loai=$_GET['loai'];
		//echo "aloai: ".$loai;
		$kq=$knoi->queryCsdl("select * from loaitintuc where loai='$loai'");
		if(mysql_num_rows($kq)<1)
		{
			echo "<br><label class='stylelitle' style='text-align=center;'>Hiện tại chưa có loại tin nào!</label>";
		}
		else
		{
		?>
			<table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
				<tr class="stylelitle"  style="text-align:center; background-color:#CCFFFF"><td style="text-align:right">Stt&nbsp;</td><td style="text-align:center;">Mã loại</td><td>Tên loại</td><td>Chi tiết</td><td>Xóa</td></tr>
		<?php
			$i=1;
			while($n=mysql_fetch_array($kq))
			{
				if($i%2==0)
			$color='#FFFFCC';
		else
			$color='#FFFFFF';
		?>
			<tr  class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td align="right"><?php echo $i; ?>&nbsp;</td>
			<td align="center"><?php echo $n['maloaitt']; ?></td><td><?php echo $n['tenloai']; ?></td><td align="center"><a  href="home_admin.php?module=loaitintuc&maloaitt=<?php echo $n['maloaitt']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết loại tin tức" /></a></td><td style="text-align:center"><a href="home_admin.php?module=xoa&field=maloaitt&table=loaitintuc&value=<?php echo $n['maloaitt']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa loại tin tức" /></a></td></tr>
			<?php
			$i++;
			}
		?>
		</table>
		<?php
		}
		?>
		<br /><label style="text-align:center" class="button"><a href="home_admin.php?module=loaitintuc&loai=<?php echo $loai; ?>"><img src="images/add.png" border="0">&nbsp;Loại tin tức mới</a></label>
		<?php
	}
	else if(isset($_GET['maloai'])) //div=id_tintuc
	{
		$maloai=$_GET['maloai'];
		//echo "maloai: ".$maloai;
		$kq=$knoi->queryCsdl("select * from tintuc where maloaitt='$maloai'");
		if(mysql_num_rows($kq)<1)
		{
			echo "<br><label class='stylelitle' style='text-align=center;'>Hiện tại chưa có tin nào!</label><br>";
		}
		else
		{
			$tongdong=mysql_num_rows($kq);
			$dong_trang=9;
			$sotrang=ceil($tongdong/$dong_trang);
			$limit="0,".$dong_trang;
			if(isset($_GET['page']))
			{
				$t=$_GET['page'];
				$ar=explode(":",$t);
				$limit=$ar[1];
			}
			//echo $limit;
			if($sotrang==1)
				$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc where maloaitt='$maloai'");
			else
				$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc where maloaitt='$maloai' limit $limit");
		?>
		  <table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
					<tr class="stylelitle"  style="text-align:center; background-color:#CCFFFF"><td style="text-align:right">Stt&nbsp;</td><td style="text-align:center;">Tiêu đề</td><td>Ngày đăng</td><td>Trạng thái</td><td>Chi tiết</td><td>Xóa</td></tr>
		<?php
			$i=1;
			if(isset($_GET['i']))
				$i=$_GET['i'] + 1;
			while($n=mysql_fetch_array($kq))
			{
				if($i%2==0)
					$color='#FFFFCC';
				else
					$color='#FFFFFF';
		?>
					<tr  class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td align="right"><?php echo $i; ?>&nbsp;</td><td><?php echo $n['tieude']; ?></td>
					<td align="center">
					<?php 
						$date=$n['ngaydang'];
						$tam=explode(" ",$date);
						$date=$tam[0];
						$date=$knoi->formatDate($date); 
						echo $date; 
						//echo "maloai";
					?>						</td>
					<td align="center"><?php if($n['trangthai']==0) echo "Lưu"; else echo "Đăng"; ?></td><td style="text-align:center"><a  href="home_admin.php?module=chitiettintuc&matt=<?php echo $n['matt']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết loại tin tức" /></a></td>
					<td style="text-align:center;"><a href="home_admin.php?module=xoa&field=matt&table=tintuc&value=<?php echo $n['matt']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa tin tức" /></a></td></tr>
				<?php
					$i++;
					}
				?>
		  </table>
			<?php
		if($sotrang>1)
		{
		if($ar[0]!=1 && isset($_GET['page']))
			$color='#0033CC';
		else
			$color='#000000';
		$start="1:0,".$dong_trang;
		?>
		<div> Pages &nbsp;
		<?php
			echo "<a href='home_admin.php?module=danhsachtintuc&loai=$loai&page=$start' style='color:$color'>1</a>";
		for($i=2;$i<=$sotrang;$i++)
		{
			if($ar[0]==$i)
				$color='#000000';
			else
				$color='#0033CC';
			$p=($i-1)*$dong_trang;
			$page=$i.":".$p.",".$dong_trang;
			echo "&nbsp;<a href='home_admin.php?module=danhsachtintuc&loai=$loai&page=$page&i=$p' style='color:$color'>$i</a>";	
		}
		?>
		</div>
		<?php
		}
		}
	  ?>
		<br /><label style="text-align:center" class="button"><a href="home_admin.php?module=tintuc&loai=<?php echo $loai; ?>&loaitin=<?php echo $madt; ?>"><img src="images/add.png" border="0">&nbsp;Tin tức mới</a></label>
		<?php
	
	
	}
	else 
	{
		$loai=$_GET['loaitintuc'];
		//echo "loai: ".$loai;
			$k=$knoi->queryCsdl("select maloaitt,tenloai from loaitintuc where loai='$loai' limit 0,1");
			$t=mysql_fetch_array($k);
			$madt=$t['maloaitt'];
			$k=$knoi->queryCsdl("select maloaitt,tenloai from loaitintuc where loai='$loai' and maloaitt!='$madt'");
		?>
		<label class="stylelitle">Loại tin tức:</label>&nbsp;<select name="loaitintuc" onChange="showtheoloai('id_tintuc',this,'maloai')">
			<option selected="selected" value="<?php echo $madt; ?>"><?php echo $t['tenloai']; ?></option>
		<?php
			while($n=mysql_fetch_array($k))
			{
		?>
				<option value="<?php echo $n['maloaitt']; ?>"><?php echo $n['tenloai']; ?></option>
		<?php
			}
		?>
		</select>
		<div id="id_tintuc" align="center">
		  <?php
				$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc where maloaitt='$madt'");
				if(mysql_num_rows($kq)<1)
				{
					echo "<br><label class='stylelitle'>Hiện tại chưa có tin tức nào!</label><br>";
				}
				else
				{
				$tongdong=mysql_num_rows($kq);
				$dong_trang=9;
				$sotrang=ceil($tongdong/$dong_trang);
				
				$limit="0,".$dong_trang;
				if(isset($_GET['page']))
				{
					$t=$_GET['page'];
					$ar=explode(":",$t);
					$limit=$ar[1];
				}
				//echo $limit;
				if($sotrang==1)
					$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc where maloaitt='$madt'");
				else
					$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc where maloaitt='$madt' limit $limit");
			?>
		  <table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
					<tr class="stylelitle"  style="text-align:center; background-color:#CCFFFF"><td style="text-align:right">Stt&nbsp;</td><td style="text-align:center;">Tiêu đề</td><td>Ngày đăng</td><td>Trạng thái</td><td>Chi tiết</td><td>Xóa</td></tr>
				<?php
					$i=1;
					if(isset($_GET['i']))
						$i=$_GET['i'] + 1;
					while($n=mysql_fetch_array($kq))
					{
						if($i%2==0)
							$color='#FFFFCC';
						else
							$color='#FFFFFF';
				?>
					<tr  class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td align="right"><?php echo $i; ?>&nbsp;</td><td><?php echo $n['tieude']; ?></td>
					<td align="center">
					<?php 
						$date=$n['ngaydang'];
						$tam=explode(" ",$date);
						$date=$tam[0];
						$date=$knoi->formatDate($date); 
						echo $date;
						//echo "loaitintuc"; 
					?>						</td>
					<td align="center"><?php if($n['trangthai']==0) echo "Lưu"; else echo "Đăng"; ?></td><td style="text-align:center"><a  href="home_admin.php?module=chitiettintuc&matt=<?php echo $n['matt']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết loại tin tức" /></a></td>
					<td style="text-align:center;"><a href="home_admin.php?module=xoa&field=matt&table=tintuc&value=<?php echo $n['matt']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa tin tức" /></a></td></tr>
				<?php
					$i++;
					}
				?>
		  </table>
			<?php
		if($sotrang>1)
		{
		if($ar[0]!=1 && isset($_GET['page']))
			$color='#0033CC';
		else
			$color='#000000';
		$start="1:0,".$dong_trang;
		?>
		<div> Pages &nbsp;
		<?php
			echo "<a href='home_admin.php?module=danhsachtintuc&loai=$loai&page=$start' style='color:$color'>1</a>";
		for($i=2;$i<=$sotrang;$i++)
		{
			if($ar[0]==$i)
				$color='#000000';
			else
				$color='#0033CC';
			$p=($i-1)*$dong_trang;
			$page=$i.":".$p.",".$dong_trang;
			echo "&nbsp;<a href='home_admin.php?module=danhsachtintuc&loai=$loai&page=$page&i=$p' style='color:$color'>$i</a>";	
		}
		?>
		</div>
		<?php
		}
	  ?>
		
<?php
}
?>
<br /><label style="text-align:center" class="button"><a href="home_admin.php?module=tintuc&loai=<?php echo $loai; ?>&loaitin=<?php echo $madt; ?>"><img src="images/add.png" border="0">&nbsp;Tin tức mới</a></label>
		</div>
<?php
}
?>
 
		